/**
 * Created by WYH on 17/4/27.
 */
angular.module('tribalc').directive('authMove', function () {
    return {
        link: function ($scope, element, attrs) {

            setupAuthDraggle();
            setupUnAuthDraggle();

            function setupAuthDraggle() {
                var $auth = $('.auth-draggle');
                var dragOffset;
                $auth.draggable({
                    revert: true,
                    start: function () {
                        $('.loau-auth-list').css('overflow', 'visible');

                    },
                    drag: function () {
                        dragOffset = $(this).offset();
                    },
                    stop: function () {
                        if (isInclude($("#unAuthBox"), $(this), dragOffset)) {
                            var company = angular.element(this).scope().company;
                            $scope.$apply(function () {
                                $scope.$parent.authCompanyList = deleteAuthCompany($scope.$parent.authCompanyList ,company);
                                $scope.$parent.unAuthCompanyList.push(company);
                            });
                        }
                        $('.loau-auth-list').css('overflow', 'auto');
                    }
                });
            }

            function setupUnAuthDraggle() {
                var $unAuth = $(".un-auth-draggle");
                var dragOffset;
                $unAuth.draggable({
                    revert: true,
                    start: function () {
                        $('.loau-auth-list').css('overflow', 'visible')
                    },
                    drag: function () {
                        dragOffset = $(this).offset();
                    },
                    stop: function () {
                        if (isInclude($("#authBox"), $(this), dragOffset)) {
                            var company = angular.element(this).scope().company;
                            $scope.$apply(function () {
                                $scope.$parent.unAuthCompanyList = deleteAuthCompany($scope.$parent.unAuthCompanyList, company);
                                $scope.$parent.authCompanyList.push(company);
                            });
                        }
                        $('.loau-auth-list').css('overflow', 'auto');
                    }
                });
            }


            function isInclude(parent, child, childOffSet) {
                var childWidth = child.width();
                var parentHeight = parent.height();
                var parentWidth = parent.width();
                var parentOffSet = parent.offset();
                return childOffSet.left + childWidth > parentOffSet.left && childOffSet.top > parentOffSet.top && childOffSet.top < parentOffSet.top + parentHeight && parentOffSet.left + parentWidth > childOffSet.left;
            }

            function deleteAuthCompany(companyList ,company) {
                var newCompanyList = [];
                companyList.forEach(function (data) {
                    if (data.id != company.id || data.companyName != company.companyName) {
                        newCompanyList.push(data);
                    }
                });
                return newCompanyList;
            }



        }
    }
});
